package com.hzsxy.bighomework.serviceImpl;

import com.hzsxy.bighomework.api.StudentClassView;
import com.hzsxy.bighomework.entity.ClassInfoDO;
import com.hzsxy.bighomework.entity.StudentDO;
import com.hzsxy.bighomework.entity.StudentClassDO;
import com.hzsxy.bighomework.entity.Teacher;
import com.hzsxy.bighomework.repository.ClassInfoRepository;
import com.hzsxy.bighomework.repository.StudentClassRepository;
import com.hzsxy.bighomework.repository.StudentRepository;
import com.hzsxy.bighomework.repository.TeacherRepository;
import com.hzsxy.bighomework.service.ClassManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClassManagementServiceImpl implements ClassManagementService {
    @Autowired
    private ClassInfoRepository classInfoRepository;
    @Autowired
    private StudentClassRepository studentClassRepository;
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private TeacherRepository teacherRepository;


    @Override
    public List<StudentClassView> getStudentClass(String classId) {
        List<StudentClassDO> studentClassList= (List<StudentClassDO>) studentClassRepository.findByClassId(classId);
        return studentClassList.stream().map(it -> {
            StudentClassView view = new StudentClassView();
            List<ClassInfoDO> classInfoDOList = classInfoRepository.findByClassId(it.getClassId());
            if (!CollectionUtils.isEmpty(classInfoDOList)){
                view.setTeacherId(classInfoDOList.get(0).getTeacherIdFK().getTeacher_id());
                view.setTeacherName(classInfoDOList.get(0).getTeacherIdFK().getName());
                view.setClassName(classInfoDOList.get(0).getClassName());
            }
            view.setClassId(it.getClassId());
            view.setStudentId(it.getStudentId());
            List<StudentDO> studentDOList = studentRepository.findByStudentId(it.getStudentId());
            if (! CollectionUtils.isEmpty(studentDOList)){
                view.setStudentName(studentDOList.get(0).getName());
            }
            return view;
        }).collect(Collectors.toList());
    }

    @Override
    public void saveClass(String classId, String className, String teacherId,int max) {
        Assert.isTrue(!StringUtils.isEmpty(classId),"课程编号不能为空");
        Assert.isTrue(!StringUtils.isEmpty(className),"课程名称不能为空");
        Assert.isTrue(!StringUtils.isEmpty(teacherId),"教师编号不能为空");
        Teacher teacher = teacherRepository.findOne(teacherId);
        Assert.notNull(teacher,String.format("找不到该老师,编号为%s",teacherId));
        ClassInfoDO classInfoDO = new ClassInfoDO();
        if (max <= 0){
            classInfoDO.setMax(-1);
        }else {
            classInfoDO.setMax(max);
        }
        classInfoDO.setClassId(classId);
        classInfoDO.setClassName(className);
        classInfoDO.setTeacherIdFK(teacher);
        classInfoRepository.save(classInfoDO);
    }
}
